<%@page import="com.wanmait.spacesys.vo.Post"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<meta charset="UTF-8">
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<%
String path = request.getContextPath();
%>
<%
Post post = (Post) request.getAttribute("post");
%>
<style>
#greybackground {
	background-color: white;
	border-radius: 10px;
	opacity: 0.5;
}

.answer {
	height: 50px;
}

.because {
	height: 50px;
}

.r {
	font-size: 15px;
	cursor: pointer;
}

.morecomment {
	font-size: 15px;
	cursor: pointer;
}

.reple {
	font-size: 14px;
	cursor: pointer;
}

.packup {
	font-size: 15px;
	cursor: pointer;
}
</style>
<script src="<%=path%>/js/jquery-3.6.0.min.js" type="text/javascript"></script>
<link href="<%=path%>/css/main5152.css" rel="stylesheet">
<jsp:include page="/inc/header.jsp"></jsp:include>
<div class="page-container">
	<div class="container">
		<div class="row">
			<div class="span8 page-content">
				<ul class="breadcrumb">
					<li></li>
					<li></li>
					<li></li>
				</ul>
				<article class=" type-post format-standard hentry clearfix">
					<h1 class="post-title"><%=post.getTitle()%></h1>
					<div class="post-meta clearfix">
						<span class="date"><%=post.getPubTime()%></span> <span
							class="comments"><a href="#"><%=post.getUserinfo().getNickname()%></a></span>
						<span class="like-count" onclick="praised(${post.id},this)"><%=post.getPraised()%></span>
					</div>
					<%=post.getContent()%>
				</article>
				<section id="comments">
					<h3 id="comments-title">评论</h3>
					<div id="comment">
						<%-- 在这里commentajax.jsp局部刷新 --%>
					</div>
					<div id="respond">
						<h3>我要评论</h3>
						<div class="cancel-comment-reply">
							<a rel="nofollow" id="cancel-comment-reply-link" href="#"
								style="display: none;">Click here to cancel reply.</a>
						</div>
						<form
							action="<%=path%>/PostController?action=addComment&id=<%=post.getId()%>"
							method="post" id="commentform">
							<p class="comment-notes" id="comment-notes">
								输入您的信息 <span class="required">*</span>
							</p>
							<div>
								<label for="comment">评论</label>
								<textarea class="span8" name="content" id="comment" cols="58"
									rows="10"></textarea>
							</div>
							<div>
								<input class="btn" name="submit" type="submit" id="submit"
									value="发表">
							</div>
						</form>
					</div>
				</section>
			</div>
			<aside class="span4 page-sidebar">
				<section class="widget">
					<div class="support-widget">
						<h3 class="title">
							<a href="<%=path%>/web/release.jsp">我要发布</a>
						</h3>
						<p class="intro">一起和航友们来分享你的见解吧~</p>

					</div>
				</section>
				<section class="widget">
					<h3 class="title">论坛文章</h3>
					<ul class="articles">
						<c:forEach items="${randomPosts }" var="post">
							<li class="article-entry standard">
								<h4>
									<a
										href="<%= path %>/PostController?action=postSingle&id=${post.id}">${post.title }</a>
								</h4> <span class="article-meta">发布时间:${post.pubTime } <a
									href="#">&amp; ${post.userinfo.nickname }</a></span> <span
								praise="false" id="praised" class="like-count" 
								onclick="praised(${post.id},this)">${post.praised }</span>
							</li>
						</c:forEach>
					</ul>
				</section>
			</aside>
		</div>
	</div>
</div>
<jsp:include page="/inc/footer.jsp"></jsp:include>
<script type="text/javascript">
	var path = "${pageContext.request.contextPath}";
	var str = '';
	$(function(){
		$("#comment").load(path+"/PostController?action=commentAjaxList&id=${post.id}");
	});
	var dataSource;
	$(function(){
		$.getJSON(path+"/PostController?action=downCommentAjax&id=${post.id}",function f(data){
			dataSource = data;
		});
		$("#comment").on("click",".morecomment",downComment);	//显示评论
		$("#comment").on("click","span.packup",packup);			//收起回复
		$("#comment").on("click",".report>span.r",report);		//添加举报事件
		$("#comment").on("click",".but",but);		//提交举报
		$("#comment").on("click","i.reple",reple);	//添加回复事件	
		$("#comment").on("click",".butt",butt);		//提交回复
		$("#comment").on("click",".out",out);		//收起举报
		$("#comment").on("click",".cut",stop); 	//收起回复
		$("#commentform").submit(submit);//点击发表判断是否禁言
	});
	//判断禁言
	function submit(){
		var days ;
		$.ajax({
			type : "post", 
			url:path+"/PostController?action=spack",
			async : false, 
			success:function(data){
				days = data;
			}
		});
		if(days>=0){
			return true;
		}
		else{
			days = Math.abs(days)
			window.alert("您已被禁言，距解封还剩"+days+"天");
			return false;			
		}
	}
	//回复事件
	function butt(){
		var postId = "${post.id}";
		var commentPid = $(".butt").attr("commentPid"); 
		var repleContent = $(this).parent().find('.answer').val();
		$(".repleSuccess").load(path+"/PostController?action=reple&commentPid="+commentPid+"&repleContent="+repleContent+"&postId="+postId);
		location.reload(true);
	}
	//回复
	function reple(){
		var commentPid = $(this).data("id");
		var rel = $(this).parent();
		var inp = $(rel).find("input");
		if(inp.length==0){
			var text = "<div id='stop'><input class='answer' placeholder='输入回复' type='text' style='height:50'><button class='butt' commentPid='"+commentPid+"'>回复</button><button class='cut'>收起</button><div><div class='repleSuccess'></div>";
			rel.append(text);
		}
		else{
			$("#stop").css("display","block");
		}
	}
	//收起回复
	function stop(){
		var cut = $(this).parent();
		$(cut).css("display","none");
	}
	//举报事件
	function but(){
 		var commentId = $(".but").attr("commentId");
 		var because = $(this).parent().find('.because').val();
 		$(".success").load(path+"/PostController?action=report&commentId="+commentId+"&because="+because);
	}
	//举报
	function report(){
		var commentId = $(this).data("id");
		var obj = $(this).parent();
		var inpu = obj.find("input");
		if(inpu.length==0){
			obj.fadeIn("fast");
		　	obj.append("<div class='greybackground'><input style='height:50' class='because' placeholder='输入举报理由' type='text'>&nbsp;<button class='but' commentId="+commentId+">提交</button><button class='out'>收起</button></div><div class='success'></div>");
		}
		else{
			$(".greybackground").css("display","block");
		}
	}
	//收起举报
	function out(){
		var out = $(this).parent().parent().find(".greybackground");
		$(out).css("display","none");
	}
	//收起评论
	function packup(){
		var rem = $(this).parent().find("div.content");
		rem.remove();
		$(this).remove();
	}
	//显示评论
	function downComment(){
		var uls = $(this).parent().find("ul");
		var temp = $(this).parent();
		if(uls.length!=0){	
			return;
		}
		var index = $(this).parent().attr("v");
		var div = $("<div class='content'></div>");
		const getStr = function(list){
			if(list.subComment!=undefined){
				var name = list.userinfo.nickname;
				list.subComment.forEach(function(row){
					var t = $("<ul class='children'><div id='pohto' class='comment-meta'><img src='"+path+"/images/"+row.userinfo.headerPic+"' height='60' width='60' class='avatar avatar-60 photo'></div><h5 class='author'><cite class='fn'>"+row.userinfo.nickname+"<span class='un'>>"+name+"</span></cite></h5><p class=data><time datetime='2013-02-26'>评论时间："+row.commentTime+"</time></p><div calss='comment-body'>"+row.content+"</div><div id='report' class='report'><img src='"+path+"/images/%E4%B8%BE%E6%8A%A5.png'><span class='r'>举报</sapn>&nbsp;&nbsp;<i style='color:black' class='reple' data-id='"+row.id+"' data-pid='"+row.pid+"'>回复</i></div></ul></div>");
					//$(".un").load(path+"/PostController?action=upCommentName&pid="+row.pid);
					div.append(t);
	                getStr(row);
				});
			}
	    }
		temp.append("<span class='packup'>Λ收起</span>");
		temp.append(div);
		getStr(dataSource[index]);
		
	}
	//点赞
	function praised(id,but){	//帖子Id
		var user = "${userinfo.username}";
		var praisedBoolean = $(".like-count").attr("praise");
		if(user==""){
			location.href=path+"/web/userLogin.jsp";
		} 
		else{
			if(praisedBoolean=="false"){	//等于false去执行点赞加一的方法
				$(this).load(path+"/PostController?action=praised&id="+id)
				$(but).html($(but).html()*1+1);
				console.log("执行加一方法");
				$(".like-count").attr("praise","true");
			}
			else{	//等于true去执行取消点赞的方法
				$(this).load(path+"/PostController?action=cancelPraised&id="+id)
				$(but).html($(but).html()*1-1); 
				console.log("执行减一方法");
				$(".like-count").attr("praise","false");
			}
		}
	}
</script>